﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VSSTQH.DataAccess;
using VSSTQH.Models;
using CrystalDecisions.CrystalReports.Engine;
using Excel = Microsoft.Office.Interop.Excel;
using Pomina.Libs;



namespace VSSTQH.Reports
{
    public partial class FrmViewReport : Form
    {
        int sort = -1;
        IList<POModel> poList;
        public FrmViewReport()
        {
            InitializeComponent();

            IProductDAO dao = new ProductDAO();
            IList<ProductModel> products = dao.GetAll();
            
            if (products != null)
            {
                products.Insert(0, new ProductModel() { ProductID = "Chọn hàng hóa ..." });
                this.cboProduct.DataSource = products;
                this.cboProduct.ValueMember = "ProductName";
                this.cboProduct.DisplayMember = "ProductID";

            }

            cboProduct.ValueChanged += new EventHandler(cboProduct_SelectedValueChanged);

            IVendorDAO dao1 = new VendorDAO();
            IList<VendorModel> vendors = dao1.GetAll();
            if (vendors != null)
            {
                vendors.Insert(0, new VendorModel() { VendorID = "Chọn khách hàng ..." });
                this.cboVendor.DataSource = vendors;
                this.cboVendor.ValueMember = "VendorName";
                this.cboVendor.DisplayMember = "VendorID";

            }

            cboVendor.ValueChanged += new EventHandler(cboVendor_SelectedValueChanged);

            this.btnViewReport.Click += new EventHandler(btnViewReport_Click);
            this.btnExcel.Click += new EventHandler(btnExcel_Click);
            this.btnClose.Click += new EventHandler(delegate { this.Close(); });

            rdoSummary.CheckedChanged += new EventHandler(rdoSummary_CheckedChanged);
        }
        
        void btnExcel_Click(object sender, EventArgs e)
        {
            IPODAO dao = new PODAO();
            string vendorID = String.Empty;
            string productID = String.Empty;
            string poID = txtPOID.Text;
            string vehicleID = txtVehicleID.Text;

            if (cboVendor.Value != null)
            {
                vendorID = cboVendor.Text;
            }

            if (cboProduct.Value != null)
            {
                productID = cboProduct.Text;
            }
            if (rdoNgay.Checked)
            {
                sort = 1;
            }
            else if (rdoSortMH.Checked)
            {
                sort = 2;
            }
            else if (rdoSortNCC.Checked)
            {
                sort = 3;
            }
            else if (rdoSortPO.Checked)
            {
                sort = 4;
            }
            else if (rdoSortWeight.Checked)
            {
                sort = 5;
            }
            else if (rdoSortCategory.Checked)
            {
                sort = 6;
            }
            if (sort < 0)
            {
                poList = dao.Search(this.dateTimePicker1.Value, this.dateTimePicker2.Value,
                   vendorID, productID, cboPoType.SelectedIndex, vehicleID, poID).OrderBy(p => p.POCode).ToList();
            }
            else
            {
                if (rdoNhapHang.Checked)
                {
                    poList = dao.SortSearch(this.dateTimePicker1.Value, this.dateTimePicker2.Value,
                       1, sort);
                }
                else if (rdoXuatHang.Checked)
                {
                    poList = dao.SortSearch(this.dateTimePicker1.Value, this.dateTimePicker2.Value,
                       2, sort);
                }
            }
            IList<TitleClass> titles = new List<TitleClass>();
            List<HeaderClass> headers = new List<HeaderClass>();

            if (poList != null && poList.Count > 0)
            {
                //ReportDocument report = null;
                string times1 = String.Empty, times2 = String.Empty;

                switch (cboPoType.SelectedIndex)
                {
                    case 1:
                        times1 = "TL Hàng và xe";
                        times2 = "TL Xe không";
                        break;
                    case 2:
                        times1 = "TL Xe không";
                        times2 = "TL Hàng và xe";
                        break;
                    default:
                        times1 = "Lần cân 1";
                        times2 = "Lần cân 2";
                        break;
                }

                if (rdoDate.Checked)
                {
                    //report = new crReportDate();
                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO THEO NGÀY",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });

                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Số phiếu", DataMember="POCode" },
                        new HeaderClass() { Text = "Số xe", DataMember="VehicleID" },
                        new HeaderClass() { Text = "Khách hàng", DataMember="VendorName" },
                        new HeaderClass() { Text = "Số PO", DataMember="PoID" },
                        new HeaderClass() { Text = "Hàng hóa", DataMember="ProductName" },
                        new HeaderClass() { Text = times1, DataMember="FirstWeight" },
                        new HeaderClass() { Text = times2, DataMember="SecondWeight" },
                        new HeaderClass() { Text = "TL Hàng", DataMember="ActualWeight" }
                    });
                }
                else if (rdoProduct.Checked)
                {
                    //report = new crReportProduct();

                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO THEO HÀNG HÓA",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });

                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Số phiếu", DataMember="POCode" },
                        new HeaderClass() { Text = "Số xe", DataMember="VehicleID" },
                        new HeaderClass() { Text = "Khách hàng", DataMember="VendorName" },
                        new HeaderClass() { Text = "Ngày giờ", DataMember="ScaleTime2" },
                        new HeaderClass() { Text = times1, DataMember="FirstWeight" },
                        new HeaderClass() { Text = times2, DataMember="SecondWeight" },
                        new HeaderClass() { Text = "TL Hàng", DataMember="ActualWeight" },
                        new HeaderClass() { Text = "Số PO", DataMember="PoID" }
                    });
                }
                else if (rdoVendor.Checked)
                {
                    //report = new crReportVendor();

                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO THEO KHÁCH HÀNG",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });

                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Số phiếu", DataMember="POCode" },
                        new HeaderClass() { Text = "Số xe", DataMember="VehicleID" },
                        new HeaderClass() { Text = "Hàng hóa", DataMember="ProductName" },
                        new HeaderClass() { Text = "Ngày giờ", DataMember="ScaleTime2" },
                        new HeaderClass() { Text = times1, DataMember="FirstWeight" },
                        new HeaderClass() { Text = times2, DataMember="SecondWeight" },
                        new HeaderClass() { Text = "TL Hàng", DataMember="ActualWeight" },
                        new HeaderClass() { Text = "Số PO", DataMember="PoID" }
                    });
                }
                else if (rdoPO.Checked)
                {
                    //report = new crReportPO();

                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO THEO SỐ PO",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });

                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Số phiếu", DataMember="POCode" },
                        new HeaderClass() { Text = "Số xe", DataMember="VehicleID" },
                        new HeaderClass() { Text = "Khách hàng", DataMember="VendorName" },
                        new HeaderClass() { Text = "Hàng hóa", DataMember="ProductName" },
                        new HeaderClass() { Text = times1, DataMember="FirstWeight" },
                        new HeaderClass() { Text = times2, DataMember="SecondWeight" },
                        new HeaderClass() { Text = "TL Hàng", DataMember="ActualWeight" }
                    });
                }
                else if (rdoVehicle.Checked)
                {
                    //report = new crReportVehicle();

                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO THEO SỐ XE",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });

                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Số phiếu", DataMember="POCode" },
                        new HeaderClass() { Text = "Khách hàng", DataMember="VendorName" },
                        new HeaderClass() { Text = "Hàng hóa", DataMember="ProductName" },
                        new HeaderClass() { Text = times1, DataMember="FirstWeight" },
                        new HeaderClass() { Text = times2, DataMember="SecondWeight" },
                        new HeaderClass() { Text = "TL Hàng", DataMember="ActualWeight" },
                        new HeaderClass() { Text = "Số PO", DataMember="PoID" }
                    });
                }
                else if(rdoNhapHang.Checked)
                {
                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO NHẬP HÀNG",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });

                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Ngày", DataMember="ScaleDate2" },
                        new HeaderClass() { Text = "Mã hàng", DataMember="ProductID" },
                        new HeaderClass() { Text = "Tên hàng", DataMember="ProductName" },
                        new HeaderClass() { Text = "Mã NCC", DataMember="VendorID" },
                        new HeaderClass() { Text = "Tên NCC", DataMember="VendorName" },
                        new HeaderClass() { Text = "Số PO", DataMember="PoID" },
                        new HeaderClass() { Text = "Số xe", DataMember="VehicleID" },
                        new HeaderClass() { Text = "Trọng lượng hàng", DataMember="ActualWeight" },
                        new HeaderClass() { Text = "Ngày vào", DataMember="ScaleTime1" },
                        new HeaderClass() { Text = "Ngày ra", DataMember="ScaleTime2" },
                        new HeaderClass() { Text = "Hạng mục", DataMember="WBS" }
                    });
                }
                else if (rdoXuatHang.Checked)
                {
                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO XUẤT HÀNG",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });

                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Ngày", DataMember="ScaleDate2" },
                        new HeaderClass() { Text = "Mã hàng", DataMember="ProductID" },
                        new HeaderClass() { Text = "Tên hàng", DataMember="ProductName" },
                        new HeaderClass() { Text = "Mã NCC", DataMember="VendorID" },
                        new HeaderClass() { Text = "Tên NCC", DataMember="VendorName" },
                        new HeaderClass() { Text = "Số PO", DataMember="PoID" },
                        new HeaderClass() { Text = "Số xe", DataMember="VehicleID" },
                        new HeaderClass() { Text = "Trọng lượng hàng", DataMember="ActualWeight" },
                        new HeaderClass() { Text = "Ngày vào", DataMember="ScaleTime1" },
                        new HeaderClass() { Text = "Ngày ra", DataMember="ScaleTime2" },
                        new HeaderClass() { Text = "Hạng mục", DataMember="WBS" }
                    });
                }
                else
                {
                    //report = new crReportSummary();

                    titles.Add(new TitleClass()
                    {
                        Row = 1,
                        Col = 0,
                        Bold = true,
                        Text = "BÁO CÁO TỔNG TRỌNG LƯỢNG NHẬP XUẤT",
                        Size = 14,
                        Merge = true,
                        Align = Excel.Constants.xlCenter
                    });
                    
                    headers.AddRange(new HeaderClass[] {
                        new HeaderClass() { Text = "Số phiếu", DataMember="POCode" },
                        new HeaderClass() { Text = "Hàng hóa", DataMember="ProductName" },
                        new HeaderClass() { Text = "Khách hàng", DataMember="VendorName" },
                        new HeaderClass() { Text = "Trọng lượng", DataMember="ActualWeight" },
                        new HeaderClass() { Text = "Loại", DataMember="StringPOType" },
                        new HeaderClass() { Text = "Ghi chú" }
                    });
                }

                //if (report != null)
                //{
                    DataTable dataTable = CollectionExtensions<POModel>.ConvertToDataTable(poList, "VSSTQH_POModel");
                    if ((rdoNhapHang.Checked)||(rdoXuatHang.Checked))
                    {
                        string fromToDate = String.Format("Từ {0:d} {0:HH:mm} Đến {1:d} {1:HH:mm}", this.dateTimePicker1.Value, this.dateTimePicker2.Value);
                        titles.Add(new TitleClass() { Row = 2, Col = 5, Bold = false, Text = fromToDate, Size = 10 });
                        ReportDB.exportDataToExcel(titles, headers, dataTable);
                        MessageBox.Show("Đã lưu Excel thành công.","Thông báo");
                    }
                    else
                    {
                        string fromToDate = String.Format("Từ {0:d} {0:HH:mm} Đến {1:d} {1:HH:mm}", this.dateTimePicker1.Value, this.dateTimePicker2.Value);
                        titles.Add(new TitleClass() { Row = 2, Col = 3, Bold = false, Text = fromToDate, Size = 10 });
                        string vendorName = "Khách: " + (cboVendor.Value == null ? "Không xác định" : cboVendor.Value.ToString());
                        titles.Add(new TitleClass() { Row = 3, Col = 3, Bold = false, Text = vendorName, Size = 10 });
                        string productName = "Hàng hóa: " + (cboProduct.Value == null ? "Không xác định" : cboProduct.Value.ToString());
                        titles.Add(new TitleClass() { Row = 3, Col = 5, Bold = false, Text = productName, Size = 10 });
                        string vID = "Số xe: " + (String.IsNullOrEmpty(vehicleID) ? "Không xác định" : vehicleID);
                        titles.Add(new TitleClass() { Row = 4, Col = 3, Bold = false, Text = vID, Size = 10 });

                        ReportDB.exportDataToExcel(titles, headers, dataTable);
                        MessageBox.Show("Đã lưu Excel thành công.","Thông báo");
                    }
                    //SettingApp setting = new SettingApp();
                    //if (Common.CheckPrinter(setting.ReceiptPrintName))
                    //    report.PrintOptions.PrinterName = setting.ReportPrintName;
                    //if (setting.IsPreviewReportPrint)
                    //{
                    //    FrmPrintPreview form = new FrmPrintPreview(report);
                    //    form.ShowDialog();
                    //}
                    //else
                    //{
                    //    report.PrintToPrinter(1, false, 0, 0);
                    //}
                //}
            }
            else
            {
                Common.ShowAlert("Từ {0:d} {0:HH:mm} Đến {1:d} {1:HH:mm} không có cân xe!", dateTimePicker1.Value, dateTimePicker2.Value);
            }
        }

        void rdoSummary_CheckedChanged(object sender, EventArgs e)
        {
            panel1.Enabled = !rdoSummary.Checked;
        }

        void btnViewReport_Click(object sender, EventArgs e)
        {
            IPODAO dao = new PODAO();
            string vendorID = String.Empty;
            string productID = String.Empty;
            string poID = txtPOID.Text;
            string vehicleID = txtVehicleID.Text;

            if (cboVendor.Value != null)
            {
                vendorID = cboVendor.Text;
            }


            if (cboProduct.Value != null)
            {
                productID = cboProduct.Text;
            }

            //Check Sort
            if (rdoNgay.Checked)
            {
                sort = 1;
            }
            else if (rdoSortMH.Checked)
            {
                sort = 2;
            }
            else if (rdoSortNCC.Checked)
            {
                sort = 3;
            }
            else if (rdoSortPO.Checked)
            {
                sort = 4;
            }
            else if (rdoSortWeight.Checked)
            {
                sort = 5;
            }
            else if (rdoSortCategory.Checked)
            {
                sort = 6;
            }

            if (sort < 0)
            {
                poList = dao.Search(this.dateTimePicker1.Value, this.dateTimePicker2.Value,
                   vendorID, productID, cboPoType.SelectedIndex, vehicleID, poID);
            }
            else
            {
                if (rdoNhapHang.Checked)
                {
                    poList = dao.SortSearch(this.dateTimePicker1.Value, this.dateTimePicker2.Value,
                       1, sort);
                }
                else if (rdoXuatHang.Checked)
                {
                    poList = dao.SortSearch(this.dateTimePicker1.Value, this.dateTimePicker2.Value,
                       2, sort);
                }
            }
            
            if (poList != null && poList.Count > 0)
            {
                ReportDocument report = null;
                if (rdoDate.Checked)
                {
                   
                    report = new crReportDate();
                }
                else if (rdoProduct.Checked)
                {
                   
                    report = new crReportProduct();
                }
                else if (rdoVendor.Checked)
                {
                    
                    report = new crReportVendor();
                }
                else if (rdoPO.Checked)
                {
                   
                    report = new crReportPO();
                }
                else if (rdoVehicle.Checked)
                {
                   
                    report = new crReportVehicle();
                }
                else if (rdoNhapHang.Checked)
                {
                    report = new crReportNhapHang();
                   
                }
                else if (rdoXuatHang.Checked)
                {
                    report = new crReportXuatHang();
                }
                else
                {
                    hidenSort(false);
                    report = new crReportSummary();
                }
                if (report != null)
                {
                    report.SetDataSource(CollectionExtensions<POModel>.ConvertToDataTable(poList, "VSSTQH_POModel"));

                    report.SetParameterValue("FromDate", String.Format("{0:d} {0:HH:mm}", this.dateTimePicker1.Value));
                    report.SetParameterValue("ToDate", String.Format("{0:d} {0:HH:mm}", this.dateTimePicker2.Value));
                    report.SetParameterValue("VendorName", cboVendor.Value == null ? "Không xác định" : cboVendor.Value.ToString());
                    report.SetParameterValue("ProductName", cboProduct.Value == null ? "Không xác định" : cboProduct.Value.ToString());
                    report.SetParameterValue("VehicleID", String.IsNullOrEmpty(vehicleID) ? "Không xác định" : vehicleID);

                    SettingApp setting = new SettingApp();
                    if (Common.CheckPrinter(setting.ReceiptPrintName))
                        report.PrintOptions.PrinterName = setting.ReportPrintName;
                    if (setting.IsPreviewReportPrint)
                    {
                        FrmPrintPreview form = new FrmPrintPreview(report);
                        form.ShowDialog();
                    }
                    else
                    {
                        report.PrintToPrinter(1, false, 0, 0);
                    }
                }
            }
            else
            {
                Common.ShowAlert("Từ {0:d} {0:HH:mm} Đến {1:d} {1:HH:mm} không có cân xe!", dateTimePicker1.Value, dateTimePicker2.Value);
            }
            
        }

        void cboVendor_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboVendor.Value != null)
            {
                txtVendorName.Text = cboVendor.Value.ToString();
            }
            else
            {
                txtVendorName.Text = String.Empty;
            }
        }

        void cboProduct_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboProduct.Value != null)
            {
                txtProductName.Text = cboProduct.Value.ToString();
            }
            else
            {
                txtProductName.Text = String.Empty;
            }
        }

        public void hidenSort(bool b)
        {
            this.rdoNgay.Enabled = b;
            this.rdoSortCategory.Enabled = b;
           
            this.rdoSortMH.Enabled = b;
            this.rdoSortNCC.Enabled = b;
           
            this.rdoSortPO.Enabled = b;
            this.rdoSortWeight.Enabled = b;
        }

        private void rdoDate_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoDate.Checked)
            {
                hidenSort(false);
                sort = -1;
            }
        }

        private void rdoProduct_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoProduct.Checked)
            {
                hidenSort(false);
                sort = -1;
            }
        }

        private void rdoVendor_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoVendor.Checked)
            {
                hidenSort(false);
                sort = -1;
            }
        }

        private void rdoVehicle_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoVehicle.Checked)
            {
                hidenSort(false);
                sort = -1;
            }
        }

        private void rdoPO_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoPO.Checked)
            {
                hidenSort(false);
                sort = -1;
            }
        }

        private void rdoSummary_CheckedChanged_1(object sender, EventArgs e)
        {
            if (rdoSummary.Checked)
            {
                hidenSort(false);
                sort = -1;
            }
        }

        private void rdoNhapHang_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoNhapHang.Checked)
            {
                hidenSort(true);
                rdoNgay.Checked = true;
            }
        }

        private void rdoXuatHang_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoXuatHang.Checked)
            {
                hidenSort(true);
                rdoNgay.Checked = true;
            }
        }

        private void FrmViewReport_Load(object sender, EventArgs e)
        {
            hidenSort(false);
        }

       

        
    }
}
